package com.zzk.lanouhn.myapplication1.fragments;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.ShowKindsDetailsActivity;
import com.zzk.lanouhn.myapplication1.adapters.MyGridViewPublishGoodAdapter;
import com.zzk.lanouhn.myapplication1.modles.PublishArticles;
import com.zzk.lanouhn.myapplication1.utils.HttpUtils;
import com.zzk.lanouhn.myapplication1.view.MyGridView;

/**
 * 作者信息->发布
 * Created by lanouhn on 16/9/10.
 */
public class AuthorsPublishFragment extends Fragment {

    private MyGridView publishView;
    private MyGridViewPublishGoodAdapter adapter;
    private PublishArticles publishArticles;
    private SharedPreferences preferences;
    private String authorId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.authorinfo_publish_fragment, null);
        publishView = (MyGridView) view.findViewById(R.id.gv_publish_good);
        initData();
        initEvent();
        getData();
        return view;
    }

    private void initData() {
        preferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        authorId = preferences.getString("authorId", "default");
    }

    private void initEvent(){
        publishView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(), ShowKindsDetailsActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("goodsid",publishArticles.getData().getPost().get(position).getId());
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    private void getData() {
        String url = "http://open4.bantangapp.com/users/center/publish?";
        String dataUrl = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d" +
                "&track_user_id=&oauth_token=&track_deviceid=863121028526879" +
                "&track_device_info=MI+2S&channel_name=xiaomi&app_installtime=1473128006" +
                "&app_versions=5.8.7&os_versions=5.0.2&screensize=720&v=19" +
                "&visit_user_id=" + authorId +
                "&page=0&pagesize=18";
        try {
            HttpUtils.doPostAsyn(url, dataUrl, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    publishArticles = gson.fromJson(result, PublishArticles.class);
                    if (publishArticles.getData().getPost().size() > 0) {
                        handler.sendEmptyMessage(1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    break;
                case 1:
                    adapter = new MyGridViewPublishGoodAdapter(getActivity(), publishArticles);
                    publishView.setAdapter(adapter);
                    break;
                default:

                    break;
            }
        }
    };
}
